import moment from 'moment'

moment.locale('zh_CN')

export function deepCopy(obj, cache = []) {
  // just return if obj is immutable value
  if (obj === null || typeof obj !== 'object') {
    return obj
  }
}
export default {
  mapTree: function (arr) {
    let lastArr = [];
    function mapTurn(arr) {
      arr.map((item => {
        if (item.childrenNodes) {
          lastArr.push({ id: item.knowledgeNodeId, pId: item.parentKnowledgeNodeId, name: item.name ? item.name : '新知识点', status: item.editStatus, sequence: item.sequence, font: { 'color': 'red' } });
          mapTurn(item.childrenNodes);
        } else {
          lastArr.push({ id: item.knowledgeNodeId, pId: item.parentKnowledgeNodeId, name: item.name ? item.name : '新知识点', status: item.editStatus, sequence: item.sequence, font: { 'color': 'red' } });
        };
      }))
    };
    mapTurn(arr);
    return lastArr;
  },
  mapReportTree: function (arr) {
    let lastArr = [];
    function mapReportTurn(arr) {
      arr.map((item => {
        if (item.childrenNodes) {
          lastArr.push(Object.assign({}, item, { id: item.knowledgeNodeId, pId: item.parentKnowledgeNodeId, name: item.name ? item.name : '新知识点' }));
          mapReportTurn(item.childrenNodes);
        } else {
          lastArr.push(Object.assign({}, item, { id: item.knowledgeNodeId, pId: item.parentKnowledgeNodeId, name: item.name ? item.name : '新知识点' }));
        };
      }))
    };
    mapReportTurn(arr);
    return lastArr;
  },
  getQueryString: function (name) {
    var r = window.location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"));
    if (r) {
      return r[2];
    }
    return null;
  },
  formatTime: function (time, format) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD H:mm:ss')
  },
  formatDate: function (time, format) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD')
  },
  firstDayOfMonth: function () {
    return moment().startOf('month');
  },
  endDayOfMonth: function () {
    return moment().endOf('month')
  },

  currentYear: function () {
    return moment().year()
  },

  currentMonth: function () {
    return moment().month() + 1
  },

  formatNumber: function (val, fixed) {
    return parseFloat(val).toFixed(fixed)
  },

  toNumber: function (val) {
    return Number(val)
  },
  isEmptyObject: function (e) {
    var t;
    for (t in e)
      return !1;
    return !0
  },
  SectionToChinese: function (section) {
    var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
    var chnUnitChar = ["", "十", "百", "千"];
    var strIns = '', chnStr = '';
    var unitPos = 0;
    var zero = true;
    while (section > 0) {
      var v = section % 10;
      if (v === 0) {
        if (!zero) {
          zero = true;
          chnStr = chnNumChar[v] + chnStr;
        }
      } else {
        zero = false;
        strIns = chnNumChar[v];
        strIns += chnUnitChar[unitPos];
        chnStr = strIns + chnStr;
      }
      unitPos++;
      section = Math.floor(section / 10);
    }
    return chnStr;
  },
  install: function (Vue) {
    Vue.filter('formatDate', this.formatDate)
    Vue.filter('formatTime', this.formatTime)
    Vue.filter('formatNumber', this.formatNumber)
    Vue.filter('toNumber', this.toNumber)
    Vue.filter('SectionToChinese', this.SectionToChinese)

    var isEmptyObject = this.isEmptyObject
    Vue.mixin({
      methods: {
        isEmptyObject: isEmptyObject,
      }
    })
  }
}
